using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.AnalysisServices.AdomdClient;
using RetailAnalytic.Core;

namespace RetailAnalytic.Tools.MDXEditor
{
    public partial class frmMDXBuild : frmTemplate
    {
        public frmMDXBuild()
        {
            InitializeComponent();
        }
        public AdomdConnection _cn ;
        public string _sCube ;
        public string _CalcMemberMDX = "";
        public string _CalcMemberName = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            //_cn = new AdomdConnection("Provider=MSOLAP.3;Data Source=CLEVER\\S2k5;Initial Catalog=Adventure Works DW;");
            metadataTreeView1.RetrieveMetadata(_cn, _sCube );
            InitializeMDXBuilder(_cn,_sCube , true, true);
            utName.Text = _CalcMemberName;
            rtbMDX.Text = _CalcMemberMDX;
           // pvt.ConnectionString = _cn.ConnectionString;
            
        }
        public void InitializeMDXBuilder(AdomdConnection _incn,string cubeName, bool isVisibleFunc, bool isVisibleTempl)
        {
            functionsListTreeView1.RetrieveFunctionsList("MDXFunctions.xml");
            //functionsListTreeView1.RetrieveFunctionsList("E:\\ReflectorsAS\\ReflectASC\\WindowsApplication1\\WindowsApplication1\\MDXFunctions.xml ");
            templatesListTreeView1.RetrieveTemplatesList("MDXTemplates.xml", Microsoft.AnalysisServices.Controls.TemplateTypes.CalculatedMember  );
            foreach (CubeDef cc in _cn.Cubes )
            {
              //  if (cc.Caption == "$Date") 
                    //.Show(cc.Caption );
               
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            //hierarchicalResultGrid1.Grid

            
        }
        public bool ValidSyntax(string sMDX, AdomdConnection _con, string sCube)
        {
            string sMDXTempl = "With member <UniqueName> as '<Expr>' select {<UniqueName>} on columns from [<Cube>]";
            AdomdCommand cl;
            string sfrmEx = sMDXTempl.Replace("<Expr>", sMDX);
            sfrmEx = sfrmEx.Replace("<Cube>", sCube);
            sfrmEx = sfrmEx.Replace("<UniqueName>", "[Measures].[WEjrwifje132kfad]");
            //hierarchicalResultGrid1.QueryString = sfrmEx;
            //hierarchicalResultGrid1.ExecuteQuery();
            cl = _cn.CreateCommand();
            cl.CommandText = sfrmEx;
            try
            {
                CellSet cs = cl.ExecuteCellSet();
                try
                {
                   object _c =  cs.Cells[0].Value;
                }
                catch (Exception _e)
                {
                    Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.MESSAGE_EMPTY,
                        Environment.StackTrace, _e));
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.MESSAGE_EMPTY, 
                    Environment.StackTrace, ex));
                return false;
            }
        }
        private void ultraButton1_Click(object sender, EventArgs e)
        {
            if (ValidSyntax(rtbMDX.Text ,_cn ,_sCube ))
            {
                Dialog.Show(Dialog_type.Information,/*SR.StringLibrary.GetStrMSG("*/
                    SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.Inf_SYNTAX_IS_SUCCESS, 
                    Environment.StackTrace, ""),
                    new ButtonDialog[] { ButtonDialog.Button_close });
            }
        }

        private void ultraButton2_Click(object sender, EventArgs e)
        {
            if (ValidSyntax(rtbMDX.Text, _cn, _sCube))
            {
                if (utName.Text.Trim() == String.Empty)
                {
                    Dialog.Show(Dialog_type.Error,/*SR.StringLibrary.GetStrMSG("*/
                        SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.Ask_INPUT_NAME_FORMUL, 
                        Environment.StackTrace, ""),
                        new ButtonDialog[] { ButtonDialog.Button_close });
                    return;
                }
                _CalcMemberName = utName.Text;
                _CalcMemberMDX = rtbMDX.Text;
                this.Close();
            }
        }

        private void ultraButton3_Click(object sender, EventArgs e)
        {
            _CalcMemberMDX = "";
            this.Close();
        }
        public bool _enableControlName
        {
            set
            {
                this.utName.Enabled = value;
            }
        }
    }
}